/**
 * 登录模块
 * @author 张云江
 * @date 2016-05-13
 */
function Application() {
    //URL配置
    this.url = {
        "add_application": APP + '/application/m_add_application',
        "edit_application": APP + '/application/m_edit_application',
        "del_application": APP + '/application/m_del_application',
        "add_subscribe": APP + '/application/m_add_subscribe',
        "edit_subscribe": APP + '/application/m_edit_subscribe',
        "del_subscribe": APP + '/application/m_del_subscribe',
    };
}

Application.prototype = {
    /**
     * 初始化
     */
    init: function () {
        var that = this;
    },

    /**
     * 提交表单
     */
    insertApplication: function () {
        var data = $("#add_application").serializeJSON();
        console.log(data);
        if (Common.isEmpty(data.application_name)) {
            layer.msg('请输入应用名称', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.build_id)) {
            layer.msg('请输入build_id', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.version)) {
            layer.msg('请输入版本号', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.min_version)) {
            layer.msg('请输入最低版本号', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: this.url.add_application,
            data: data,
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('添加应用成功',{icon:1},function(){
                        window.location.href = APP + '/Application/appmgr.html';
                    });

                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

    /**
     * 提交表单
     */
    editApplication: function () {
        var data = $("#add_application").serializeJSON();
        console.log(data);
        if (Common.isEmpty(data.application_name)) {
            layer.msg('请输入应用名称', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.build_id)) {
            layer.msg('请输入build_id', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.version)) {
            layer.msg('请输入版本号', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.min_version)) {
            layer.msg('请输入最低版本号', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: this.url.edit_application,
            data: data,
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('修改应用成功',{icon:1},function(){
                        window.location.href = APP + '/Application/appmgr.html';
                    });

                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

    /**
     * 删除应用
     */
    delApplication: function (application_id) {
        console.log(application_id);
        if (Common.isEmpty(application_id)) {
            layer.msg('应用ID错误', {icon: 5});
            return false;
        }
        $.ajax({
            type: "post",
            url: this.url.del_application,
            data: {application_id:application_id},
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('删除应用成功',{icon:1},function(){
                        window.location.href = APP + '/Application/appmgr.html';
                    });

                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

    /**
     * 提交表单
     */
    insertSubscribe: function () {
        var data = $("#add_application").serializeJSON();
        console.log(data);
        if (Common.isEmpty(data.product_name)) {
            layer.msg('请输入产品名称', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.product_channel)) {
            layer.msg('请输入渠道号', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.application_id) || data.application_id == "0") {
            layer.msg('请输入选择所属应用', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.pay)) {
            layer.msg('请输入产品金额', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.product_time)) {
            layer.msg('请输入会员时长', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: this.url.add_subscribe,
            data: data,
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('添加产品成功',{icon:1},function(){
                        window.location.href = APP + '/Application/subscribemgr.html';
                    });
                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

    /**
     * 提交表单
     */
    editSubscribe: function () {
        var data = $("#add_application").serializeJSON();
        console.log(data);
        if (Common.isEmpty(data.product_name)) {
            layer.msg('请输入产品名称', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.product_channel)) {
            layer.msg('请输入渠道号', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.application_id) || data.application_id == "0") {
            layer.msg('请输入选择所属应用', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.pay)) {
            layer.msg('请输入产品金额', {icon: 5});
            return false;
        }
        if (Common.isEmpty(data.product_time)) {
            layer.msg('请输入会员时长', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: this.url.edit_subscribe,
            data: data,
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('修改产品成功',{icon:1},function(){
                        window.location.href = APP + '/Application/subscribemgr.html';
                    });
                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

    /**
     * 删除应用
     */
    delSubscribe: function (product_id) {
        console.log(product_id);
        if (Common.isEmpty(product_id)) {
            layer.msg('产品ID错误', {icon: 5});
            return false;
        }
        $.ajax({
            type: "post",
            url: this.url.del_subscribe,
            data: {product_id:product_id},
            success: function (data) {
                if (data.code == "0") {
                    layer.msg('删除产品成功',{icon:1},function(){
                        window.location.href = APP + '/Application/subscribemgr.html';
                    });
                } else {
                    layer.msg(data.notice, {icon: 5});
                }
            }
        });
    },

};